//
//  ScrolleButton.m
//  mall
//
//  Created by megvii on 14-8-23.
//  Copyright (c) 2014年 megvii. All rights reserved.
//

#import "ScrolleButton.h"
#import "LogoModel.h"
#import "MMDAddLogoVC.h"
//#import "YTAirManageer.h"

#define kBTNTAG 100
#define kBTNMINWINTH 60
#define kBTNHIGHT 40
#define kADDBTNWINTH 50

@interface ScrolleButton ()

@property (nonatomic, strong) UIButton *nextBTN; //上一次选择的按钮
@property (nonatomic, strong) UIView *proView;  //红色滚动条

@end


@implementation ScrolleButton

- (void)dealloc
{
    self.clickBTNBlock = nil;
    //bababababba
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor ytColorWithRed:28 green:201 blue:194 alpha:1];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.alpha = 0.95;
        self.proView = [[UIView alloc] initWithFrame:CGRectMake(0, 40 - 2, 0, 2)];
        [self addSubview:self.proView];
        [self.proView setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    if (_dataArray != dataArray) {
        _dataArray = dataArray;
        CGFloat cont_x = 0;
        
        for (int i = 0; i <= dataArray.count; i++) {
            if (i < dataArray.count) {
                LogoModel *model = self.dataArray[i];
                CGSize bSize = [model.name sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}];
                if (bSize.width <= kBTNMINWINTH) {
                    bSize.width = kBTNMINWINTH;
                }
                bSize.width = bSize.width + 5;
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setTitle:model.name forState:UIControlStateNormal];
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                button.titleLabel.font = [UIFont fontWithName:@"Gill Sans" size:15];
                [button setFrame:CGRectMake(cont_x, 0, bSize.width, kBTNHIGHT)];
                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                [button setTag:kBTNTAG + i];
                [button setBackgroundColor:[UIColor clearColor]];
//                [button setShowsTouchWhenHighlighted:YES];
                [self addSubview:button];
                if (model.isNew == YES) {
                    [button setImage:[UIImage imageNamed:@"mark_read"] forState:UIControlStateNormal];
                    [button setImageEdgeInsets:UIEdgeInsetsMake(5, bSize.width - 5, kBTNHIGHT - 10, 0)];
                }

                if (i == 0) {
                    [self.proView setFrame:CGRectMake(cont_x, kBTNHIGHT - 2, bSize.width, 2)];
                }
                
                cont_x = cont_x + bSize.width;
            }else{
                if (cont_x < WIN_WIDTH - kADDBTNWINTH) {
                    cont_x = WIN_WIDTH - kADDBTNWINTH;
                }
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                [button setFrame:CGRectMake(cont_x, 0, kADDBTNWINTH, kBTNHIGHT)];
                [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
                [button setTag:kBTNTAG + i];
                [button setBackgroundColor: [UIColor clearColor]];
                [button setImage:[UIImage imageNamed:@"btn_add_b"] forState:UIControlStateNormal];
//                [button setShowsTouchWhenHighlighted:YES];
                [self addSubview:button];

                cont_x = cont_x + kADDBTNWINTH;
            }
        }
        [self setContentSize:CGSizeMake(cont_x, kBTNHIGHT)];
    }
}
//scroller的时候事件，不要外传参数
- (void)scrollerAction:(id)sender
{
    [self pubBTNAction:sender];
}

//点击事件，需要外传参数
- (void)buttonAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    
    [self pubBTNAction:button];
    if (button.tag - kBTNTAG != self.dataArray.count) {
        if (self.clickBTNBlock) {
            self.clickBTNBlock(button.tag - kBTNTAG);
        }
    }else
    {
        if (![[YTAirManageer shareAirManager] compareViewController:[MMDAddLogoVC class]]) {
            MMDAddLogoVC *addVC = [[MMDAddLogoVC alloc] initWithNibName:@"MMDAddLogoVC" bundle:nil];
            UINavigationController *naviAddVC = [[UINavigationController alloc] initWithRootViewController:addVC];
            [[YTAirManageer shareAirManager] chageAirViewController:naviAddVC animation:YES];
        }
    }
}

- (void)pubBTNAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    [button setImageEdgeInsets:UIEdgeInsetsMake(5, button.frame.size.width, kBTNHIGHT - 5, 0)];

    if (button.tag - kBTNTAG != self.dataArray.count) {
        __unsafe_unretained LogoModel *model = self.dataArray[button.tag - kBTNTAG];
        model.isNew = NO;
    }
    
    if (self.contentSize.width > WIN_WIDTH) {
        CGFloat button_x = button.center.x;
        if (button_x > WIN_WIDTH / 2) {
            if ((self.contentSize.width - button_x) > WIN_WIDTH / 2) {
                [self setContentOffset:CGPointMake(button_x - WIN_WIDTH / 2, 0) animated:YES];
            }else{
                [self setContentOffset:CGPointMake(self.contentSize.width - WIN_WIDTH, 0) animated:YES];
            }
        }else{
            [self setContentOffset:CGPointZero animated:YES];
        }
    }
    
    if (self.nextBTN && self.nextBTN != button) {
        self.nextBTN.titleLabel.font = [UIFont fontWithName:@"Gill Sans" size:15];
        
        CGRect rect = button.frame;
        rect.origin.y = kBTNHIGHT - 2;
        [UIView animateWithDuration:0.2f
                         animations:^{
            [self.proView setFrame:rect];
        }];
        
    }
    [self showBTNWithAni:button];
    
    self.nextBTN = button;
}

//设置顶部的动态偏移
- (void)setOffIndex:(NSInteger)index
{
    index = index + kBTNTAG;
    UIView *button  = [self viewWithTag:index];
    [self scrollerAction:button];
}


- (void)showBTNWithAni:(UIView *)view
{
//    [UIView animateWithDuration:0.2 animations:^{
//        CATransform3D trans = CATransform3DMakeScale(1.3, 1.3, 1);
//        view.layer.transform = trans;
//    } completion:^(BOOL finished) {
//        [UIView animateWithDuration:0.15 animations:^{
//            CATransform3D trans = CATransform3DIdentity;
//            view.layer.transform = trans;
//        } completion:^(BOOL finished)
//            
//        }];
//    }];
    UIButton *button = (UIButton *)view;
    button.titleLabel.font = [UIFont fontWithName:@"Gill Sans" size:15];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
